package com.zhongshengnetwork.rightbe.lang.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.clerk.view.ClerkFragment;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.TextUtil.DisplayUtils;
import com.zhongshengnetwork.rightbe.lang.activity.LangSearchActivity;
import com.zhongshengnetwork.rightbe.wzt.view.WZTFindFragment;

import java.util.ArrayList;
import java.util.List;

public class SimpleHomePageFragment extends BaseAnalyticsFragment implements View.OnClickListener{

    private View rootView;
    private ViewPager mPaper;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private TextView tv_one,tv_two,tv_three,tv_notice;
    private LinearLayout tv_one_line,tv_two_line,tv_three_line;
    private CareLangFragment careLangFragment;
    private HotLangFragment hotLangFragment;
    private NewLangFragment newLangFragment;
    public MainActivity mainActivity;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.simple_home_page_layout, container,false);
            initLayout();
            mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
                @Override
                public int getCount() {
                    return mFragments.size();
                }

                @Override
                public Fragment getItem(int arg0) {
                    return mFragments.get(arg0);
                }

            };
            mPaper.setAdapter(mAdapter);
            mPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                private int currentIndex = 0;

                @Override
                public void onPageSelected(int position) {
                    resetColor();
                    setSelect(position);
                    currentIndex = position;
                    if (position == 0){
                        if (careLangFragment != null){
                            SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.CareUpdateKey,"0");
                            updateCareTip(false);
                        }
                    }
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
            RelativeLayout category_layout = (RelativeLayout) rootView.findViewById(R.id.category_layout);
            category_layout.setBackgroundColor(AppThemeUtils.getInstance().getTopBg());
            category_layout.setClickable(true);
            category_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    scrollToFirstData();
                }
            });

            ImageView left_top_icon = (ImageView) rootView.findViewById(R.id.left_top_icon);
            left_top_icon.setClickable(true);
            left_top_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getActivity() != null && mainActivity != null){
                        mainActivity.enterChannel();
                    }
                }
            });

            ImageView top_top_icon = (ImageView) rootView.findViewById(R.id.top_top_icon);
            top_top_icon.setClickable(true);
            top_top_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getActivity() != null){
                        Intent intent = new Intent(getActivity(), LangSearchActivity.class);
                        getActivity().startActivity(intent);
                    }

                }
            });
            mPaper.setCurrentItem(1);

        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }


    private void setSelect(int position){
        switch (position) {
            case 0://FC6E51
                tv_one.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 18)));
                tv_one.setTextColor(AppThemeUtils.getInstance().getTopTextSelectColor());
                tv_one_line.setVisibility(View.VISIBLE);
                break;
            case 1:
                tv_two.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 18)));
                tv_two.setTextColor(AppThemeUtils.getInstance().getTopTextSelectColor());
                tv_two_line.setVisibility(View.VISIBLE);
                break;
            case 2:
                tv_three.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 18)));
                tv_three.setTextColor(AppThemeUtils.getInstance().getTopTextSelectColor());
                tv_three_line.setVisibility(View.VISIBLE);
                break;
            default:
                tv_one.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 18)));
                tv_one.setTextColor(AppThemeUtils.getInstance().getTopTextSelectColor());
                tv_one_line.setVisibility(View.VISIBLE);
                mPaper.setCurrentItem(0);
                break;
        }
    }

    /**
     * 初始化控件
     */
    public void initLayout(){
        tv_one = (TextView)rootView.findViewById(R.id.tv_one);
        tv_one_line = (LinearLayout) rootView.findViewById(R.id.tv_one_line);
        tv_one_line.setBackgroundColor(AppThemeUtils.getInstance().getTopTextSelectColor());
        tv_notice = (TextView) rootView.findViewById(R.id.tv_notice);
        tv_two = (TextView)rootView.findViewById(R.id.tv_two);
        tv_two_line = (LinearLayout) rootView.findViewById(R.id.tv_two_line);
        tv_two_line.setBackgroundColor(AppThemeUtils.getInstance().getTopTextSelectColor());
        tv_three = (TextView)rootView.findViewById(R.id.tv_three);
        tv_three_line = (LinearLayout)rootView.findViewById(R.id.tv_three_line);
        tv_three_line.setBackgroundColor(AppThemeUtils.getInstance().getTopTextSelectColor());
        mPaper = (ViewPager)rootView.findViewById(R.id.view_pager);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
        tv_three.setOnClickListener(this);

        careLangFragment = new CareLangFragment();
        mFragments.add(careLangFragment);

        hotLangFragment = new HotLangFragment();
        mFragments.add(hotLangFragment);

        newLangFragment = new NewLangFragment();
        mFragments.add(newLangFragment);

        resetColor();
        setSelect(1);

    }

    public void resetColor(){
        tv_one.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 16)));
        tv_one.setTextColor(AppThemeUtils.getInstance().getTopTextUnSelectColor());
        tv_one_line.setVisibility(View.INVISIBLE);
        tv_two.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 16)));
        tv_two.setTextColor(AppThemeUtils.getInstance().getTopTextUnSelectColor());
        tv_two_line.setVisibility(View.INVISIBLE);
        tv_three.setTextSize(DisplayUtils.px2sp(getActivity(), DisplayUtils.sp2px(getActivity(), 16)));
        tv_three.setTextColor(AppThemeUtils.getInstance().getTopTextUnSelectColor());
        tv_three_line.setVisibility(View.INVISIBLE);
    }
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_one:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
            case R.id.tv_two:
                resetColor();
                setSelect(1);
                mPaper.setCurrentItem(1);
                break;
            case R.id.tv_three:
                resetColor();
                setSelect(2);
                mPaper.setCurrentItem(2);
                break;
            default:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
        }
    }
    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<Activity> mListViews;

        public MyPagerAdapter(List<Activity> mListViews) {
            this.mListViews = mListViews;
        }


        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }



        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

    public void updateAfterLogin(){

        if (careLangFragment != null){
            careLangFragment.getFirstData(true);
        }
        if (newLangFragment != null){
            newLangFragment.updateAfterLogin();
        }

    }

    public void scrollToFirstData(){
        if (mFragments != null && mFragments.size() > 0){
            if (mPaper.getCurrentItem() == 0){
                if (careLangFragment != null){
                    careLangFragment.getFirstData(true);
                }
            }else if (mPaper.getCurrentItem() == 1){
                if (hotLangFragment != null){
                    hotLangFragment.getFirstData(true);
                }
            }else if (mPaper.getCurrentItem() == 2){
                if (newLangFragment != null){
                    newLangFragment.getFirstData(true);
                }
            }
        }
    }

    public void updateCareTip(Boolean visible){

        if (tv_notice != null && mPaper != null){
            String tip = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.CareUpdateKey,"0");
            if (tip.equals("1") && CommonUtils.isLogin() == true && mPaper.getCurrentItem() != 0){
                tv_notice.setVisibility(View.VISIBLE);
            }else {
                tv_notice.setVisibility(View.GONE);
            }
        }

    }

    public void getLatestData(){
        if (newLangFragment != null){
            newLangFragment.getNewData();
        }
    }

    public void getNewData(){
        if (mPaper != null ){
            if(mPaper.getCurrentItem() == 1){
                if (hotLangFragment != null){
                    hotLangFragment.getNewData();
                }
            }else if (mPaper.getCurrentItem() == 2){
                if (newLangFragment != null){
                    newLangFragment.updateFirstPage();
                }
            }else if (mPaper.getCurrentItem() == 0){
                if (CommonUtils.isLogin() == true && careLangFragment != null) {
                    careLangFragment.updateFirstPage();
                }
            }

        }
    }

}

